<?php

include_once($GLOBALS['fileroot'] . "/interface/globals.php");
include_once($GLOBALS["srcdir"] . "/api.inc");
include_once ($GLOBALS['fileroot'] . "/library/patient.inc");

function vitals_report($pid, $encounter, $cols, $id, $print = true) {
    $count = 0;
    $data = formFetch("form_vitals", $id);
    $patient_data = getPatientData($GLOBALS['pid']);
    $patient_age = getPatientAge($patient_data['DOB']);

    if ($data) {
        $vitals .= "<ul>";

        foreach ($data as $key => $value) {

            if ($key == "id" || $key == "pid" ||
                    $key == "user" || $key == "groupname" ||
                    $key == "authorized" || $key == "activity" ||
                    $key == "date" || $value == "" ||
                    $value == "0000-00-00 00:00:00" || $value == "0.0") {
                // skip certain data
                continue;
            }

            if ($value == "on") {
                $value = "yes";
            }

            $key = ucwords(str_replace("_", " ", $key));

            //modified by BM 06-2009 for required translation
            if ($key == "Temp Method" || $key == "BMI Status") {
                if ($key == "BMI Status") {
                    if ($patient_age <= 20 || (preg_match('/month/', $patient_age))) {
                        $value = "See Growth-Chart";
                    }
                }
                $vitals .= "<li><p><span class=title_normal>" . xl($key) . ": </span><span class=desc>" . xl($value) . "</span></p></li>";
            } elseif ($key == "Bps") {
                $bps = $value;
                if ($bpd) {
                    $vitals .= "<li><p><span class=title_normal>" . xl('Blood Pressure') . ": </span><span class=desc>" . $bps . "/" . $bpd . "</span></p></li>";
                } else {
                    continue;
                }
            } elseif ($key == "Bpd") {
                $bpd = $value;
                if ($bps) {
                    $vitals .= "<li><p><span class=title_normal>" . xl('Blood Pressure') . ": </span><span class=desc>" . $bps . "/" . $bpd . "</span></p></li>";
                } else {
                    continue;
                }
            } elseif ($key == "Weight") {
                $convValue = number_format($value * 0.45359237, 2);
                // show appropriate units
                if ($GLOBALS['units_of_measurement'] == 2) {
                    $vitals .= "<li><p><span class=title_normal>" . xl($key) . ": </span><span class=desc>" . $convValue . " " . xl('kg') . " (" . $value . " " . xl('lb') . ")</span></p></li>";
                } elseif ($GLOBALS['units_of_measurement'] == 3) {
                    $vitals .= "<li><p><span class=title_normal>" . xl($key) . ": </span><span class=desc>" . $value . " " . xl('lb') . "</span></p></li>";
                } elseif ($GLOBALS['units_of_measurement'] == 4) {
                    $vitals .= "<li><p><span class=title_normal>" . xl($key) . ": </span><span class=desc>" . $convValue . " " . xl('kg') . "</span></p></li>";
                } else { // = 1 or not set
                    $vitals .= "<li><p><span class=title_normal>" . xl($key) . ": </span><span class=desc>" . $value . " " . xl('lb') . " (" . $convValue . " " . xl('kg') . ")</span></p></li>";
                }
            } elseif ($key == "Height" || $key == "Waist Circ" || $key == "Head Circ") {
                $convValue = number_format($value * 2.54, 2);
                // show appropriate units
                if ($GLOBALS['units_of_measurement'] == 2) {
                    $vitals .= "<li><p><span class=title_normal>" . xl($key) . ": </span><span class=desc>" . $convValue . " " . xl('cm') . " (" . $value . " " . xl('in') . ")</span></p></li>";
                } elseif ($GLOBALS['units_of_measurement'] == 3) {
                    $vitals .= "<li><p><span class=title_normal>" . xl($key) . ": </span><span class=desc>" . $value . " " . xl('in') . "</span></p></li>";
                } elseif ($GLOBALS['units_of_measurement'] == 4) {
                    $vitals .= "<li><p><span class=title_normal>" . xl($key) . ": </span><span class=desc>" . $convValue . " " . xl('cm') . "</span></p></li>";
                } else { // = 1 or not set
                    $vitals .= "<li><p><span class=title_normal>" . xl($key) . ": </span><span class=desc>" . $value . " " . xl('in') . " (" . $convValue . " " . xl('cm') . ")</span></p></li>";
                }
            } elseif ($key == "Temperature") {
                $convValue = number_format((($value - 32) * 0.5556), 2);
                // show appropriate units
                if ($GLOBALS['units_of_measurement'] == 2) {
                    $vitals .= "<li><p><span class=title_normal>" . xl($key) . ": </span><span class=desc>" . $convValue . " " . xl('C') . " (" . $value . " " . xl('F') . ")</span></p></li>";
                } elseif ($GLOBALS['units_of_measurement'] == 3) {
                    $vitals .= "<li><p><span class=title_normal>" . xl($key) . ": </span><span class=desc>" . $value . " " . xl('F') . "</span></p></li>";
                } elseif ($GLOBALS['units_of_measurement'] == 4) {
                    $vitals .= "<li><p><span class=title_normal>" . xl($key) . ": </span><span class=desc>" . $convValue . " " . xl('C') . "</span></p></li>";
                } else { // = 1 or not set
                    $vitals .= "<li><p><span class=title_normal>" . xl($key) . ": </span><span class=desc>" . $value . " " . xl('F') . " (" . $convValue . " " . xl('C') . ")</span></p></li>";
                }
            } elseif ($key == "Pulse" || $key == "Respiration" || $key == "Oxygen Saturation" || $key == "BMI") {
                $value = number_format($value, 0);
                if ($key == "Oxygen Saturation") {
                    $vitals .= "<li><p><span class=title_normal>" . xl($key) . ": </span><span class=desc>" . $value . " " . xl('%') . "</span></p></li>";
                } elseif ($key == "BMI") {
                    $vitals .= "<li><p><span class=title_normal>" . xl($key) . ": </span><span class=desc>" . $value . " " . xl('kg/m^2') . "</span></p></li>";
                } else { //pulse and respirations
                    $vitals .= "<li><p><span class=title_normal>" . xl($key) . ": </span><span class=desc>" . $value . " " . xl('per min') . "</span></p></li>";
                }
            } elseif ($key == "Bodyfatpercent") {
                $vitals .= "<li><p><span class=title_normal>" . "Porcentaje de grasa corporal" . ": </span><span class=desc>" . $value . " " . xl('%') . "</span></p></li>";
            } elseif ($key == "Bodyfatrange") {
                $vitals .= "<li><p><span class=title_normal>" . "Nivel de grasa corporal" . ": </span><span class=desc>" . $value . " " . xl('%') . "</span></p></li>";
            } elseif ($key == "Bodywaterpercent") {
                $vitals .= "<li><p><span class=title_normal>" . "Porcentaje de agua en el cuerpo" . ": </span><span class=desc>" . $value . " " . xl('%') . "</span></p></li>";
            } elseif ($key == "Bodywatermass") {
                $vitals .= "<li><p><span class=title_normal>" . "Masa de agua en el cuerpo" . ": </span><span class=desc>" . $value . " " . xl('%') . "</span></p></li>";
            } elseif ($key == "Visceralfatrating") {
                $vitals .= "<li><p><span class=title_normal>" . "Nivel de grasa viceral" . ": </span><span class=desc>" . $value . " " . xl('%') . "</span></p></li>";
            } elseif ($key == "Dailycalorieintake") {
                $vitals .= "<li><p><span class=title_normal>" . "Consumo cal&oacute;rico diario" . ": </span><span class=desc>" . $value . " " . xl('%') . "</span></p></li>";
            } elseif ($key == "Metabolicage") {
                $vitals .= "<li><p><span class=title_normal>" . "Edad metab&oacute;lica" . ": </span><span class=desc>" . $value . " " . xl('%') . "</span></p></li>";
            } elseif ($key == "Musclemass") {
                $vitals .= "<li><p><span class=title_normal>" . "Masa muscular" . ": </span><span class=desc>" . $value . " " . xl('%') . "</span></p></li>";
            } elseif ($key == "Physiquerating") {
                $vitals .= "<li><p><span class=title_normal>" . "Condici&oacute;n f&iacute;sica" . ": </span><span class=desc>" . $value . " " . xl('%') . "</span></p></li>";
            } elseif ($key == "Bonemass") {
                $vitals .= "<li><p><span class=title_normal>" . "Masa &oacute;sea" . ": </span><span class=desc>" . $value . " " . xl('%') . "</span></p></li>";
            } elseif ($key == "Fatmass") {
                $vitals .= "<li><p><span class=title_normal>" . "Masa grasa" . ": </span><span class=desc>" . $value . " " . xl('%') . "</span></p></li>";
            } elseif ($key == "Fatfreemass") {
                $vitals .= "<li><p><span class=title_normal>" . "Masa libre de grasa" . ": </span><span class=desc>" . $value . " " . xl('%') . "</span></p></li>";
            } elseif ($key == "Basalmetabolicrate") {
                $vitals .= "<li><p><span class=title_normal>" . "Tasa metabolica basal" . ": </span><span class=desc>" . $value . " " . xl('%') . "</span></p></li>";
            } else {
                $vitals .= "<li><p><span class=title_normal>" . xl($key) . ": </span><span class=desc>$value</span></p></li>";
            }

            $count++;

            if ($count == $cols) {
                $count = 0;
            }
        }

        $vitals .= "</ul>";
    }
    if ($print)
        echo $vitals;
    else
        return $vitals;
}
?> 
